package com.zt.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.io.Serializable;

@Data
@TableName("orders")
public class Order implements Serializable {
    @TableId(type = IdType.AUTO)
    private Integer id;

    private String orderNo;
    private Integer userId;
    private Integer courseId;
    private String courseName;
    private Integer teacherId;
    private BigDecimal amount;
    private Integer couponId;
    private BigDecimal discountAmount;
    private Integer status; // 1-待付款，2-进行中，3-已完成，4-已取消
    private LocalDateTime createdAt;
    private LocalDateTime paidAt;
    private LocalDateTime finishedAt;
    @TableField("booking_id")
    private Integer bookingId;

    // 创建订单的静态方法
    public static Order createOrder(Integer userId, Integer courseId, Integer teacherId,
                                    BigDecimal amount, Integer couponId, BigDecimal discountAmount, String courseName,Integer bookingId) {
        Order order = new Order();
        order.setOrderNo(generateOrderNo());
        order.setUserId(userId);
        order.setCourseId(courseId);
        order.setTeacherId(teacherId);
        order.setAmount(amount);
        order.setCouponId(couponId);
        order.setDiscountAmount(discountAmount);
        order.setStatus(1); // 待付款
        order.setCreatedAt(LocalDateTime.now());
        order.setCourseName(courseName);
        order.setBookingId(bookingId);
        return order;
    }

    // 生成订单号
    private static String generateOrderNo() {
        return String.format("%s%06d",
                LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss")),
                (int)(Math.random() * 1000000));
    }
}